package com.erp.erp_servers.Dictionary.imp;

import com.erp.erp_dao.DictionaryKey.IDictionaryKeyDAO;

import com.erp.erp_dao.DictionaryValue.IDictionaryValueDAO;
import com.erp.erp_entitys.Department.DepartmentEntity;
import com.erp.erp_entitys.DictionaryKey.DictionaryKeyEntity;
import com.erp.erp_entitys.DictionaryKey.DictionaryKeyEntityExample;

import com.erp.erp_entitys.DictionaryKey.DictionaryTreeEntity;
import com.erp.erp_servers.Dictionary.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpDictionaryServer
 * @Description 字典服务实现类
 * @Author songjizhen
 * @Date 2020-06-22 14:49
 * @Version 1.0
 */
@Service
public class ImpDictionaryService implements IDictionaryService {
    @Autowired
    IDictionaryKeyDAO  dictionaryKeyDAO;
    @Autowired
    IDictionaryValueDAO dictionaryValueDAO;
    /**
     * 根据code获取字典key值
     * @param code
     * @return
     */
    @Override
    public DictionaryKeyEntity selectKeyByCode(int code) {
        return dictionaryKeyDAO.selectByPrimaryKey(code);
    }
    /**
     * 根据主键更新字典
     * @param record
     * @return
     */
    @Override
    public int updateKeyByCode(DictionaryKeyEntity record) {
        try {
            return dictionaryKeyDAO.updateByPrimaryKeySelective(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
    /**
     * 插入字典数据
     * @param record
     * @return
     */
    @Override
    public int insertKey(DictionaryKeyEntity record) {
        try {
            return dictionaryKeyDAO.insertSelective(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
    /**
     * 获取所有字典
     * @return
     */
    @Override
    public List<DictionaryKeyEntity> selectKeyAll() {
        DictionaryKeyEntityExample de=new DictionaryKeyEntityExample();
        return dictionaryKeyDAO.selectByExample(de);
    }

    /**
     * 获取字典树
     *
     * @return
     */
    @Override
    public DictionaryTreeEntity getDictionryTree() {
        DictionaryKeyEntityExample de=new DictionaryKeyEntityExample();
        List<DictionaryKeyEntity> dictionaryKeyEntities = dictionaryKeyDAO.selectByExample(de);
        Map<Integer,DictionaryKeyEntity> map=new HashMap<Integer,DictionaryKeyEntity>();
        for(int i=0;i<dictionaryKeyEntities.size();i++){
            map.put(dictionaryKeyEntities.get(i).getCode(),dictionaryKeyEntities.get(i));
        }
        DictionaryTreeEntity tree = this.getTree(0, map);
        return tree;
    }

    private DictionaryTreeEntity getTree(int code, Map<Integer,DictionaryKeyEntity> map){
        DictionaryTreeEntity dte=new DictionaryTreeEntity();
        dte=dte.conver(map.get(code));
        List<Integer> list=new ArrayList<Integer>();
        List<DictionaryTreeEntity> sublist=new ArrayList<DictionaryTreeEntity>();
        for (Map.Entry<Integer,DictionaryKeyEntity> entry : map.entrySet()) {
            if(dte.getCode()==entry.getValue().getCode()){
                list.add(entry.getKey());
            }
            //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        for(int i=0;i<list.size();i++){
            sublist.add(this.getTree(list.get(i),map));
        }
        dte.setList(sublist);
        return dte;
    }

    /**
     * 获取父节点下所有子节点
     * @param code
     * @return
     */
    @Override
    public List<DictionaryKeyEntity> selectKeyByParent(int code) {
        return dictionaryKeyDAO.selectKeyByParent(code);
    }

    @Override
    public String selectCodesByParent(int code) {
        return dictionaryKeyDAO.selectCodesByParent(code);
    }

}
